package com.tosit.bmx.controller.admin;

import com.tosit.bmx.common.shiro.CustomizedToken;
import com.tosit.bmx.domian.Muser;
import com.tosit.bmx.service.admin.MuserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 管理员所用的所有接口
 *
 * @author tsl
 * @create 2018-04-19 14:05
 **/
@RequestMapping("/admin")
@Controller
public class AdminCotroller {

    @Autowired
    MuserService muserService;
    /**
     * 管理员登录
     * @author tsl  
     * @date 2018/4/20 17:47  
     * @param name
     * @param pwd
     * @param model
     * @return 
     */
    @RequestMapping(value = "/login")
    public String adminLogin(@RequestParam("name") String name, @RequestParam("pwd") String pwd, Model model) {
        String resultUrl = "/admin/login";
        CustomizedToken usernamePasswordToken = new CustomizedToken(name, pwd, "Admin");
        Subject subject = SecurityUtils.getSubject();
        usernamePasswordToken.setRememberMe(true);
        try {
            subject.login(usernamePasswordToken);
            if (subject.isAuthenticated()) {
                Muser user = muserService.findsUserByName(name);
                subject.getSession().setAttribute("muser", user);
               // resultUrl =  "/p/admin/admin-index";
                return "redirect:/p/admin/admin-index";
            }
        } catch (AuthenticationException e) {
            if (e instanceof UnknownAccountException) {
                model.addAttribute("msg", "用户不存在");
            }
            if (e instanceof IncorrectCredentialsException) {
                model.addAttribute("msg", "密码错误");
            }
        }
        return  resultUrl;
    }
    /**
     * 管理员登出
     * @author tsl  
     * @date 2018/4/20 17:48  
     * @param 
     * @return 
     */  
    @RequestMapping("/loginOut")
    public String loginOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/p/admin/login";
    }
    /**
     *   多人并发
     * @author tsl
     * @date 2018/4/20 19:36
     * @param type
     * @return
     */
    @RequestMapping("/out")
    public String out(String type,Integer race,Model model) {
       if (type.equals("user")){
           if (race == 1) {
               model.addAttribute("s","您的账号在另一台设备上登录，您被挤下线，若不是您本人操作，请立即修改密码！");
               System.out.println();
               return "/user/login";
           }else {
               model.addAttribute("s","ཁྱེད་ཀྱི་རྩིས་ཐོའི་ཨང་གྲངས་གཞན་ཞིག་ཐའེ་ཝན་དང་སྒྲིག་ཆས་ཐོག་ར་འཛེག་ན་མི་ཁྱེད་ཀྱིས་རང་ཉིད་ཀྱི་བཀོལ་སྤྱོད་བྱོས་མྱུར་དུ་བཟོ་བཅོས་གསང་བ། ");
               return "/user/zlogin";
           }
       }else {
           model.addAttribute("s","您的账号在另一台设备上登录，您被挤下线，若不是您本人操作，请立即修改密码！");
           return "/admin/login";
       }
    }

}
